﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UCCalendario.ascx.cs" Inherits="wesiGestClinica.Controles.UCCalendario" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:TextBox ID="txtFecha" runat="server"  Width="70px" CssClass="TextoCajaIzquierda" OnBlur="javascript:valFecha(this);"></asp:TextBox>
<img id="imgFec" src="../Image/calendar.gif" alt="" runat="server" style="cursor: hand" />
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" Format="dd/MM/yyyy"  PopupButtonID="imgFec"
    TargetControlID="txtFecha" />
<cc1:MaskedEditExtender ID="MaskedEditExtender1" runat="server" ClearTextOnInvalid="false"
    DisplayMoney="Left" Mask="99/99/9999" MaskType="Date" TargetControlID="txtFecha" />

<script language="javascript" type="text/javascript">
    /*function MostrarMsj() 
    {
        valor = "<-Cerrar";
        document.getElementById('<%=this.lblMensaje.ClientID%>').value = valor;                     
    }
    
    onclick="javascript:MostrarMsj();"
    <asp:Label ID="lblMensaje" runat="server" Text=" "/>
    */
    
    function valFecha(oTxt)
    {        
        var bOk = true;
        
        if (oTxt.value =="01/01/0001")
            oTxt.value ="__/__/____";            
            
        if (oTxt.value != "__/__/____")
        {
            bOk = bOk && (valDia(oTxt));
            bOk = bOk && (valMes(oTxt));
            bOk = bOk && (valAno(oTxt));           
            
            
            bOk = bOk && (valSep(oTxt));
            
            
            if (!bOk)
            {
                alert("Fecha inválida");
                oTxt.value = "";
                oTxt.focus();
            }
        }
    } 
    function valDia(oTxt)
    {
        var bOk = false;
        var nDia = parseInt(oTxt.value.substr(0, 2), 10);
        bOk = bOk || ((nDia >= 1) && (nDia <= finMes(oTxt)));
        return bOk;
    }

    function valMes(oTxt){
        var bOk = false;
        var nMes = parseInt(oTxt.value.substr(3, 2), 10);
        bOk = bOk || ((nMes >= 1) && (nMes <= 12));
        return bOk;
    }

    function valAno(oTxt)
    {
        var bOk = true;
        var nAno = oTxt.value.substr(6);
        bOk = bOk && (nAno.length == 4); 
        if (bOk)
        {
            for (var i = 0; i < nAno.length; i++)
            {
                bOk = bOk && esDigito(nAno.charAt(i));
            }
        }
        return bOk;
    }
    function valSep(oTxt){
        var bOk = false;
        bOk = bOk || ((oTxt.value.charAt(2) == "/") && (oTxt.value.charAt(5) == "/"));
        return bOk;
    }
    function esDigito(sChr)
    {
        var sCod = sChr.charCodeAt(0);
        return ((sCod > 47) && (sCod < 58));
    }
    function finMes(oTxt)
    {
        var nMes = parseInt(oTxt.value.substr(3, 2), 10);
        var nRes = 0;
        switch (nMes)
        {
            case 1: nRes = 31; break;
            case 2: 
                if (parseInt(oTxt.value.substr(6)) % 4 == 0)
                {
                    nRes = 29;
                }
                else
                {
                    nRes = 28; 
                }
                
                break;
            case 3: nRes = 31; break;
            case 4: nRes = 30; break;
            case 5: nRes = 31; break;
            case 6: nRes = 30; break;
            case 7: nRes = 31; break;
            case 8: nRes = 31; break;
            case 9: nRes = 30; break;
            case 10: nRes = 31; break;
            case 11: nRes = 30; break;
            case 12: nRes = 31; break;
        }
        return nRes;
    }
</script>